package com.cskaoyan.wordmemorize.controller.app;

import com.cskaoyan.wordmemorize.auth.StpKit;
import com.cskaoyan.wordmemorize.common.result.Result;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.dto.app.PayOrderDTO;
import com.cskaoyan.wordmemorize.dto.app.VocPaySaleDTO;
import com.cskaoyan.wordmemorize.request.PageRequest;
import com.cskaoyan.wordmemorize.request.PaySuccessNotifyCommand;
import com.cskaoyan.wordmemorize.request.VocPaySaleCommand;
import com.cskaoyan.wordmemorize.service.UserPayService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("user/pay")
public class AppUserPayController {


    @Resource
    UserPayService userPayService;

    /**
     * 获取购买记录
     * @param pageRequest 分页查询请求
     * @return PageDTO<VocPaySaleDTO> 购买记录分页结果
     */
    @GetMapping("list")
    public Result<PageDTO<VocPaySaleDTO>> payedList(PageRequest pageRequest) {
//        VocPaySaleDTO
        PageDTO<VocPaySaleDTO> paySaleHistory = userPayService.getPaySaleHistory(StpKit.USER.getLoginIdAsLong(), pageRequest);
        return Result.ok(paySaleHistory);
    }

    /**
     * 购买功能
     * @param command 购买信息请求体
     * @return payOrderDTO 支付信息
     */
    @PostMapping()
    public Result<PayOrderDTO> pay(@RequestBody VocPaySaleCommand command) {
        PayOrderDTO payOrderDTO = userPayService.pay(command, StpKit.USER.getLoginIdAsLong());
        return Result.ok(payOrderDTO);
    }

    /**
     *
     * @param command 支付成功通知请求体
     * @return success
     */
    @PostMapping("notice")
    public String paySuccess(@RequestBody PaySuccessNotifyCommand command) {
        userPayService.paySuccess(command, command.getUserId());
        // 收到消息就表示已经支付成功了  返回字符串即可
        return "success"; // TODO 到底返回什么
    }

    @GetMapping("{paySaleId}")
    public Result<Integer> getPaySale(@PathVariable("paySaleId") Long paySaleId) {
        Integer code = userPayService.queryTradeStatus(paySaleId);
        return Result.ok(code);
    }

}
